﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//new
using System.IO;
using System.Xml;
using System.Configuration;
using System.Net.NetworkInformation;
using HK;
using DHNetSDK;
using System.Drawing;
using System.Drawing.Imaging;

namespace JKZP
{
    public class PP
    {
        /// <summary>
        /// 监控球机类型
        /// </summary>
        public enum JKCAMERATYPE { HAIKANG,DAHUA };
        /// <summary>
        /// 图片合成模式
        /// </summary>
        public enum PICJIONMODE { NONE = 0, 不合成 = 1 };
        /// <summary>
        /// 文件夹保存方式
        /// </summary>
        public enum FOLDERSAVEMODE
        {
            按路口名称保存,
            按年月日保存,
            按年保存,
            按月保存,
            按日保存,
            按路口代码保存,
            按摄像机IP保存,
            按方向保存,
            按摄像机类型保存,
            按摄像机名称保存
        };
        /// <summary>
        /// 图片保存方式
        /// </summary>
        public enum PICNAMEMODE
        {
            国标代码,
            指定字符,
            路口代码,
            方向代码,
            车道代码,
            年,
            月,
            日,
            时,
            分,
            秒,
            毫秒,
            路口名称,
            车道名称,
            车牌号码,
            违法行为代码,
            抓拍员,
            摄像机IP,
            方向名称,
            摄像机类型,
            抓拍张数,
            连拍序号,
            图片格式
        };
        /// <summary>
        /// 摄像机处于路口的位置
        /// </summary>
        public enum CAMERAPOSITION { 东 = 1, 南, 西, 北 };

        /// <summary>
        /// 监控摄像机类型
        /// </summary>
        public class CameraType
        {
            /// <summary>
            /// 摄像机类型名称
            /// </summary>
            public string typename;
            /// <summary>
            /// 单张图片像素宽度
            /// </summary>
            public string picwidth;
            /// <summary>
            /// 单张图片像素高度
            /// </summary>
            public string picheight;
            /// <summary>
            /// 项目地点
            /// </summary>
            public string city;
            /// <summary>
            /// 一组图片抓拍数目
            /// </summary>
            public string snapcount;
            /// <summary>
            /// 图片合成模式
            /// </summary>
            public PICJIONMODE picjionmode;
            /// <summary>
            /// 备注
            /// </summary>
            public string bz;
        }
        /// <summary>
        /// 路口参数
        /// </summary>
        public class RoadParam
        {
            /// <summary>
            /// 路口ID
            /// </summary>
            public string LKID;
            /// <summary>
            /// 国标代码
            /// </summary>
            public string GBDM;
            /// <summary>
            /// 路口名称
            /// </summary>
            public string LKMC;
            /// <summary>
            /// 部门编号
            /// </summary>
            public string BMBH;
            /// <summary>
            /// 用户分配
            /// </summary>
            public string USER;
            /// <summary>
            /// 路段
            /// </summary>
            public string LuDuan;
            /// <summary>
            /// 摄像机列表
            /// </summary>
            public List<JKCamera>  JKCamList;
        }


        /// <summary>
        /// 杰瑞尾部图片信息长度
        /// </summary>
        public const int JARIINFOLENGTH = 165;
        /// <summary>
        /// 杰瑞图片尾部信息结构体
        /// </summary>
        public class structJariPicInfo
        {
            public structJariPicInfo()
            {
                m_Info1 = "";
                m_Info2 = "";
                m_Info3 = "";
                m_Info4 = "";
                m_Info5 = "";
                m_Info6 = "";
                m_Info7 = "";
                m_Info8 = "";
                m_Info9 = "";
                m_Info10 = "";
                m_Info11 = "";
                m_Info12 = "";
                m_Info13 = "";
                m_Info14 = "";
                m_Info15 = "";
                m_Info16 = "";
                m_Info17 = "";
                m_Info18 = "";
                m_Info19 = "";
                m_Info20 = "";
                m_Info21 = "";
                m_Info22 = "";
                m_Info23 = "";
                m_Info24 = "";
            }
            /// <summary>
            /// 预留，长度19
            /// </summary>
            public string m_Info1;
            /// <summary>
            /// 图片序号，长度1
            /// 值0-2
            /// </summary>
            public string m_Info2;
            /// <summary>
            /// 旋转标识，长度1
            /// 0---不旋转
            /// 1---旋转
            /// </summary>
            public string m_Info3;
            /// <summary>
            /// 车牌号码，长度16
            /// </summary>
            public string m_Info4;
            /// <summary>
            /// 车牌颜色，长度1
            /// 0---蓝色
            /// 1---黄色
            /// 2---白色
            /// 3---黑色
            /// </summary>
            public string m_Info5;
            /// <summary>
            /// 车牌类型,长度1
            /// 0---标准民用与军车车牌
            /// 1---02 式民用车牌
            /// 2---武警车牌
            /// 3---警车车牌
            /// 4---民用车双行尾牌
            /// </summary>
            public string m_Info6;
            /// <summary>
            /// 车身颜色，长度1
            /// 0---其他
            ///1---白色
            ///2---银色
            ///3---灰色
            ///4---黑色
            ///5---红色
            ///6---深蓝
            ///7---蓝色
            ///8---黄色
            ///9---绿色
            ///10---棕色
            ///11---粉色
            ///12---紫色
            /// </summary>
            public string m_Info7;
            /// <summary>
            /// 车身颜色深浅，长度1
            /// 0---深色
            /// 1---浅色
            /// </summary>
            public string m_Info8;
            /// <summary>
            /// 车辆类型，长度1
            /// 0---其他
            /// 1---小型车辆
            /// 2---大型车辆
            /// </summary>
            public string m_Info9;
            /// <summary>
            /// 违法记录类型，长度1
            /// 杰瑞规定：
            /// 0---卡口
            /// 1---逆行
            /// 2---超速
            /// 3---闯红灯
            /// 4---压线(协议新增)
            /// 5---不按导线行驶(协议新增)
            /// 海康规定：
            /// 0-正常：1-低速，2-超速，3-逆行，4-闯红灯，5-压车道线，6-不按导向 
            /// </summary>
            public string m_Info10;
            /// <summary>
            /// 车长，长度1
            /// 精确到0.1 米
            /// </summary>
            public string m_Info11;
            /// <summary>
            /// 路口名称，长度32
            /// </summary>
            public string m_Info12;
            /// <summary>
            /// 拍摄车道，长度1
            /// 取值范围1-4 和摄像机接线有关
            /// </summary>
            public string m_Info13;
            /// <summary>
            /// 识别位置，长度48
            /// 识别位置:一个车道对应
            ///两个识别区域共16 个字
            ///节；每个识别区域8 个字
            ///节，分别存储“Left ，
            ///Right，Top，Bottom”值
            ///（每个值2 个字节）；
            ///77+0~77+15 为第一个车
            ///道，依次类推；
            /// </summary>
            public string m_Info14;
            /// <summary>
            /// 车道,长度1，在抓拍当前车道时，其余车道过车信息
            /// </summary>
            public string m_Info15;
            /// <summary>
            /// 毫秒，长度2
            /// </summary>
            public string m_Info16;
            /// <summary>
            /// 违法地点，长度2，地点代码
            /// </summary>
            public string m_Info17;
            /// <summary>
            /// 违法时间，长度19，YYYY-MM-DD HH:MM:SS
            /// </summary>
            public string m_Info18;
            /// <summary>
            /// 行驶方向，长度1，
            /// 1- 由东向西
            /// 2- 由南向北
            /// 3- 由西向东
            /// 4- 由北向南
            /// </summary>
            public string m_Info19;
            /// <summary>
            /// 车道，长度5
            /// 实际车道标识，取值如下：车道1、车道2、车道3
            /// </summary>
            public string m_Info20;
            /// <summary>
            /// 限速，长度3，KM/h
            /// </summary>
            public string m_Info21;
            /// <summary>
            /// 车速，长度3，KM/h
            /// </summary>
            public string m_Info22;
            /// <summary>
            /// ID号，长度3,0-999
            /// </summary>
            public string m_Info23;
            /// <summary>
            /// 摄像机类型，长度1
            /// 0---普清
            /// 1---JR200 万高清
            /// 2---JR500 万高清
            /// 3---JR200 万高清旋转
            /// 6---JH200 万高清
            /// 7---JH500 万高清
            /// 9---车辆牌照
            /// </summary>
            public string m_Info24;
        }

        public class User
        {
            public string username;
            public string password;
            public bool admin;
            public User()
            {
                username = "";
                password = "";
                admin = false;
            }
        }

        public static string gWorkFolder;
        public static string gConfigFile;
        public static string gRoadFile;
        public static string gWFXWDMFile;
        public static string gDPSDKWFDDFile;
        public static string gLuDuanFile;
        public static string gHPZLFile;
        public static string gJKSavePath;
        public static string gJKRecordPath;
        public static bool gJKRecordEnable;
        public static bool gJARITailEnable;
        public static bool gUploadPicOnly;
        public static string gTempFolder;
        public static string gLocalIP;
        public static bool gSoftRegstor;
        public static string gRegNode;
        public static User gUser;
        public static List<string> gJKFolderTypeList;
        public static List<string> gJKNameTypeList;
        public static List<string> gJKNameTextList;
        public static bool gJKSaveBySubFolder;
        public static string gDefaultHPHM;
        public static List<CameraType> gCameraTypeList;
        public static List<RoadParam> gRoadList;
        public static bool gHKSDKInit;
        public static bool gDHSDKInit;
        public const int cPreSetPointNum=10;
        

        //网络版升级
        public static bool gISWebVersion;
        public static string gServerIP;
        public static string gVersion;
        public static aitmskdws.aitmskdws kdws;
        public static string gUserXml;


        //DPSDK升级
        public static System.Collections.Hashtable gDPSDK_WFDD;
        public static List<string> gLuDuanList;


        public static void CreateFolder()
        {
            string w = gWorkFolder + "\\WebConfig\\";
            string f = gWorkFolder + "\\Config\\";
            string l = gWorkFolder + "\\Log\\";
            gTempFolder = gWorkFolder + "\\Temp\\";

            if (!Directory.Exists(w))
            {
                Directory.CreateDirectory(w);
            }
            if (!Directory.Exists(f))
            {
                Directory.CreateDirectory(f);
            }
            if (!Directory.Exists(l))
            {
                Directory.CreateDirectory(l);
            }
            if (!Directory.Exists(gTempFolder))
            {
                Directory.CreateDirectory(gTempFolder);
            }
        }
        public static void LoadSysParam()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                if (File.Exists(gConfigFile))
                {
                    doc.Load(gConfigFile);
                    gJKRecordPath = ReadInnerTextString(doc, "JKRecordPath");
                    if (!Directory.Exists(gJKRecordPath)) Directory.CreateDirectory(gJKRecordPath);
                    gJKRecordEnable = bool.Parse(ReadInnerTextString(doc, "JKRecordEnable"));
                    gUploadPicOnly = bool.Parse(ReadInnerTextString(doc, "UploadPicOnly"));
                    gJARITailEnable = bool.Parse(ReadInnerTextString(doc, "JARITailEnable"));
                    gJKSavePath = ReadInnerTextString(doc, "JKSavePath");
                    if (!Directory.Exists(gJKSavePath)) Directory.CreateDirectory(gJKSavePath);
                    gLocalIP = ConfigurationManager.AppSettings.Get("LocalIP");
                    gRegNode = ReadInnerTextString(doc, "RegNode");
                    gDefaultHPHM = ReadInnerTextString(doc, "DefaultHPHM");
                    gJKFolderTypeList = ReadAllAttributeName(doc, "JKSubFolder");
                    gJKNameTypeList = ReadAllSubNodeAttribute0Names(doc, "JKPicName");
                    gJKNameTextList = ReadAllSubNodeAttribute0Values(doc, "JKPicName");
                    gJKSaveBySubFolder = bool.Parse(ReadAttributeString(doc, "JKSaveBySubFolder", "enable"));
                }
            }
            catch
            {

            }
        }
        public static void ReadUserNameAndPassword()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                if (File.Exists(gConfigFile))
                {
                    doc.Load(gConfigFile);
                    gUser.username = ReadInnerTextString(doc, "username");
                    gUser.password = ReadInnerTextString(doc, "password");
                    gUser.admin = true;
                }
            }
            catch
            {

            }
        }
        public static bool SavePassWord(string password)
        {
            return SetConfig(gConfigFile, "descendant::password", password, null, null);
        }

        public static System.Collections.Hashtable LoadDPSDKWFDD()
        {
            XmlDocument doc = new XmlDocument();
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            try
            {
                if (File.Exists(gDPSDKWFDDFile))
                {
                    doc.Load(gDPSDKWFDDFile);
                    XmlNodeList wfddnodes = doc.SelectNodes("descendant::wfdd");
                    if (wfddnodes != null)
                    {
                        foreach (XmlNode node in wfddnodes)
                        {
                            string camid = node.Attributes["camid"].Value;
                            string wfdddm = node.Attributes["wfdddm"].Value;
                            ht[camid] = wfdddm;
                        }
                    }
                }
            }
            catch {  }
            return ht;

        }

        public static List<string> LoadLuDuan()
        {
            List<string> ans = new List<string>();
            try
            {
                XmlDocument doc = new XmlDocument();
                if (File.Exists(gLuDuanFile))
                {
                    doc.Load(gLuDuanFile);
                    XmlNodeList ldnodes = doc.SelectNodes("descendant::luduan");
                    if (ldnodes != null)
                    {
                        foreach (XmlNode node in ldnodes)
                        {
                            ans.Add(node.InnerText);
                        }
                    }
                }
            }
            catch { }
            return ans;
        }

        public static List<CameraType> LoadCamType()
        {
          //  <cameratype typename="DAHUA" picwidth="1600" picheight="1240" city="睢宁" snapcount="3" picjionmode="不合成" bz="">
          //</cameratype>
          //<cameratype typename="HAIKANG" picwidth="2448" picheight="2080" city="睢宁" snapcount="3" picjionmode="不合成" bz="">
          //</cameratype>
            XmlDocument doc = new XmlDocument();
            List<CameraType> camtypelist = new List<CameraType>();
            camtypelist.Clear();
            try
            {
                if (File.Exists(gConfigFile))
                {
                    doc.Load(gConfigFile);
                    XmlNodeList cameratypenode = doc.SelectNodes("descendant::cameratype");
                    if (cameratypenode != null)
                    {
                        foreach (XmlNode node in cameratypenode)
                        {
                            CameraType ct = new CameraType();
                            ct.typename = node.Attributes["typename"].Value;
                            ct.picwidth = node.Attributes["picwidth"].Value;
                            ct.picheight = node.Attributes["picheight"].Value;
                            ct.city = node.Attributes["city"].Value;
                            ct.snapcount = node.Attributes["snapcount"].Value;
                            ct.picjionmode = (PICJIONMODE)Enum.Parse(typeof(PICJIONMODE), node.Attributes["picjionmode"].Value);
                            ct.bz = node.Attributes["bz"].Value;
                            camtypelist.Add(ct);
                        }
                    }
                }
            }
            finally
            {
                doc.RemoveAll();
            }
            return camtypelist;
        }
        public static List<RoadParam> LoadRoadParam()
        {
            //<road roadid="20" gbdm="" roadname="测试路口">
            //    <camera cameraname="测试路口东球机" ip="192.168.1.108" type="DAHUA" position="东" bz="">
            //    </camera>
            //    <camera cameraname="测试路口西球机" ip="192.168.1.122" type="HAIKANG" position="西" bz="">
            //    </camera>
            //  </road>
            XmlDocument doc = new XmlDocument();
            List<RoadParam> roadlist = new List<RoadParam>();
            roadlist.Clear();
            try
            {
                if (File.Exists(gRoadFile))
                {
                    doc.Load(gRoadFile);
                    XmlNodeList roadlistnode = doc.SelectNodes("descendant::road[contains(@user,'" + PP.gUser.username + "-')]");
                    if (roadlistnode != null)
                    {
                        foreach (XmlNode roadnode in roadlistnode)
                        {
                            RoadParam param = new RoadParam();
                            param.LKID = roadnode.Attributes["roadid"].Value;
                            param.GBDM = roadnode.Attributes["gbdm"].Value;
                            param.LKMC = roadnode.Attributes["roadname"].Value;
                            param.BMBH = roadnode.Attributes["bmbh"].Value;
                            param.USER = roadnode.Attributes["user"].Value;
                            try
                            {
                                param.LuDuan = roadnode.Attributes["luduan"].Value;
                            }
                            catch
                            {
                                param.LuDuan = "";
                            }
                            param.JKCamList = LoadCamList(roadnode, param.LKID, param.LKMC, param.GBDM, param.BMBH);
                            roadlist.Add(param);
                        }
                    }
                }
            }
            catch(Exception er)
            {
                Console.WriteLine(er.Message);
            }
            finally
            {
                doc.RemoveAll();
            }
            return roadlist;

        }
        private static List<JKCamera> LoadCamList(XmlNode roadnode,string roadid,string roadname,string gbdm,string bmbh)
        {
          //  <road roadid="20" gbdm="" roadname="测试路口">
          //  <camera cameraname="测试路口东球机" ip="192.168.1.108" type="DAHUA" position="东" speed="4" bz="">
            //<PreSetPoint index="1">录像原点</PreSetPoint>
            //<PreSetPoint index="2"></PreSetPoint>
            //<PreSetPoint index="3"></PreSetPoint>
            //<PreSetPoint index="4"></PreSetPoint>
            //<PreSetPoint index="5"></PreSetPoint>
            //<PreSetPoint index="6"></PreSetPoint>
            //<PreSetPoint index="7"></PreSetPoint>
            //<PreSetPoint index="8"></PreSetPoint>
            //<PreSetPoint index="9"></PreSetPoint>
            //<PreSetPoint index="10"></PreSetPoint>
          //  </camera>
          //  <camera cameraname="港城大道-昌意路" ip="172.24.40.188" type="HAIKANG" position="东" bz="">
          //  <camera cameraname="测试路口西球机" ip="192.168.1.122" type="HAIKANG" position="西" bz="">
          //  </camera>
          //</road>
            List<JKCamera> camList = null;
            try
            {
                camList = new List<JKCamera>();
                camList.Clear();
                if ((roadnode != null) && (roadnode.ChildNodes != null))
                {
                    foreach (XmlNode camnode in roadnode.ChildNodes)
                    {
                        JKCAMERATYPE t = (JKCAMERATYPE)Enum.Parse(typeof(JKCAMERATYPE), camnode.Attributes["type"].Value);
                        if (t == JKCAMERATYPE.DAHUA)
                        {
                            DHJKCamera dhcam = new DHJKCamera();
                            dhcam.m_RoadID = roadid;
                            dhcam.m_RoadName = roadname;
                            dhcam.m_GBDM = gbdm;
                            dhcam.m_BMBH = bmbh;
                            dhcam.m_deviceID = camnode.Attributes["deviceid"].Value;
                            dhcam.m_CameraName = camnode.Attributes["cameraname"].Value;
                            dhcam.m_Position = (CAMERAPOSITION)Enum.Parse(typeof(CAMERAPOSITION), camnode.Attributes["position"].Value);
                            dhcam.m_IP = camnode.Attributes["ip"].Value;
                            dhcam.m_bz = camnode.Attributes["bz"].Value;
                            dhcam.m_UserName = camnode.Attributes["username"].Value;
                            dhcam.m_PassWord = camnode.Attributes["password"].Value;
                            dhcam.m_PreSetPonitHelpName = ReadAllSubNodeInnerText(camnode);
                            camList.Add(dhcam);
                        }
                        else if (t == JKCAMERATYPE.HAIKANG)
                        {
                            HKJKCamera hkcam = new HKJKCamera();
                            hkcam.m_RoadID = roadid;
                            hkcam.m_RoadName = roadname;
                            hkcam.m_GBDM = gbdm;
                            hkcam.m_BMBH = bmbh;
                            hkcam.m_deviceID = camnode.Attributes["deviceid"].Value;
                            hkcam.m_CameraName = camnode.Attributes["cameraname"].Value;
                            hkcam.m_Position = (CAMERAPOSITION)Enum.Parse(typeof(CAMERAPOSITION), camnode.Attributes["position"].Value);
                            hkcam.m_IP = camnode.Attributes["ip"].Value;
                            hkcam.m_bz = camnode.Attributes["bz"].Value;
                            hkcam.m_UserName = camnode.Attributes["username"].Value;
                            hkcam.m_PassWord = camnode.Attributes["password"].Value;
                            hkcam.m_PreSetPonitHelpName = ReadAllSubNodeInnerText(camnode);
                            camList.Add(hkcam);
                        }
                    }
                }
            }
            catch
            {
                camList = null;
            }
            return camList;
        }
        
        /// <summary>
        /// 从图片尾部获取尾部信息byte[]
        /// </summary>
        /// <param name="PicByte">带尾部信息的图片byte[]</param>
        /// <returns>尾部信息byte[]</returns>
        public static byte[] GetTailFromPic(byte[] PicByte)
        {
            byte[] tail = null;
            try
            {
                if ((PicByte != null) && (PicByte.Length > JARIINFOLENGTH))
                {
                    int l = PicByte.Length;
                    tail = new byte[JARIINFOLENGTH];
                    for (int i = 0; i < JARIINFOLENGTH; i++)
                    {
                        tail[i] = PicByte[l - (JARIINFOLENGTH - i)];
                    }
                }
            }
            catch
            {
                tail = null;
            }
            return tail;
        }
        /// <summary>
        /// 生成杰瑞尾部图片byte[]
        /// </summary>
        /// <param name="info">尾部信息结构体</param>
        /// <returns>尾部信息byte[]</returns>
        public static byte[] MakeTailFromInfo(structJariPicInfo info)
        {
            byte[] tail = null;
            try
            {
                tail = new byte[JARIINFOLENGTH];
                Encoding.Default.GetBytes(info.m_Info1, 0, info.m_Info1.Length, tail, 0);
                Encoding.Default.GetBytes(info.m_Info2, 0, info.m_Info2.Length, tail, 19);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info3, 0, info.m_Info3.Length, tail, 20);//睢宁不需要
                Encoding.Default.GetBytes(info.m_Info4, 0, info.m_Info4.Length, tail, 21);
                Encoding.Default.GetBytes(info.m_Info5, 0, info.m_Info5.Length, tail, 37);
                //Encoding.Default.GetBytes(info.m_Info6, 0, info.m_Info6.Length, tail, 38);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info7, 0, info.m_Info7.Length, tail, 39);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info8, 0, info.m_Info8.Length, tail, 40);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info9, 0, info.m_Info9.Length, tail, 41);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info10, 0, info.m_Info10.Length, tail, 42);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info11, 0, info.m_Info11.Length, tail, 43);//睢宁不需要
                Encoding.Default.GetBytes(info.m_Info12, 0, info.m_Info12.Length, tail, 44);
                //Encoding.Default.GetBytes(info.m_Info13, 0, info.m_Info13.Length, tail, 76);//睢宁不需要
                Encoding.Default.GetBytes(info.m_Info14, 0, info.m_Info14.Length, tail, 77);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info15, 0, info.m_Info15.Length, tail, 125);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info16, 0, info.m_Info16.Length, tail, 126);//睢宁不需要
                Encoding.Default.GetBytes(info.m_Info17, 0, info.m_Info17.Length, tail, 128);
                Encoding.Default.GetBytes(info.m_Info18, 0, info.m_Info18.Length, tail, 130);
                //Encoding.Default.GetBytes(info.m_Info19, 0, info.m_Info19.Length, tail, 149);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info20, 0, info.m_Info20.Length, tail, 150);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info21, 0, info.m_Info21.Length, tail, 155);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info22, 0, info.m_Info22.Length, tail, 158);//睢宁不需要
                //Encoding.Default.GetBytes(info.m_Info23, 0, info.m_Info23.Length, tail, 161);//协议只有3位，这里是5位，暂时不做
                //Encoding.Default.GetBytes(info.m_Info24, 0, info.m_Info24.Length, tail, 164);//睢宁不需要
            }
            catch
            {
                tail = null;
            }
            return tail;
        }
        public static byte[] ChangeBmpFileToJpgBytes(string bmpfile)
        {
            byte[] ans = null;
            MemoryStream ms = new MemoryStream();
            Image jpg = null;
            try
            {
                if (File.Exists(bmpfile))
                {
                    jpg = Image.FromFile(bmpfile);
                    if (jpg != null)
                    {
                        jpg.Save(ms, ImageFormat.Jpeg);
                        ans = ms.ToArray();
                    }
                }
            }
            catch
            {
                ans = null;
            }
            finally
            {
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
                if (jpg != null)
                {
                    jpg.Dispose();
                }
            }
            return ans;
        }
        
        /// <summary>
        /// 添加尾部信息到图片
        /// </summary>
        /// <param name="info">尾部信息</param>
        /// <param name="pic">图片byte[]</param>
        /// <returns>带尾部信息的图片byte[]</returns>
        public static byte[] AddTailToPic(structJariPicInfo info, byte[] pic)
        {
            byte[] ans = null;
            try
            {
                byte[] tail = MakeTailFromInfo(info);
                if ((tail != null) && (pic != null))
                {
                    int tailLength = tail.Length;
                    int picLength = pic.Length;
                    ans = new byte[tailLength + picLength];
                    for (int i = 0; i < picLength; i++)
                    {
                        ans[i] = pic[i];
                    }
                    for (int i = 0; i < tailLength; i++)
                    {
                        ans[picLength + i] = tail[i];
                    }
                }
            }
            catch
            {
                ans = null;
            }
            return ans;
        }
        /// <summary>
        /// 从带尾部信息的图片中获取信息
        /// </summary>
        /// <param name="PicByte">带尾部信息的图片</param>
        /// <returns>图片尾部信息结构体</returns>
        public static structJariPicInfo GetInfoFromPic(byte[] PicByte)
        {
            structJariPicInfo jaristruct = new structJariPicInfo();
            try
            {
                byte[] tail = GetTailFromPic(PicByte);
                if (tail != null)
                {
                    jaristruct.m_Info1 = Encoding.Default.GetString(tail, 0, 19);
                    jaristruct.m_Info2 = Encoding.Default.GetString(tail, 19, 1);
                    jaristruct.m_Info3 = Encoding.Default.GetString(tail, 20, 1);
                    jaristruct.m_Info4 = Encoding.Default.GetString(tail, 21, 16);
                    jaristruct.m_Info5 = Encoding.Default.GetString(tail, 37, 1);
                    jaristruct.m_Info6 = Encoding.Default.GetString(tail, 38, 1);
                    jaristruct.m_Info7 = Encoding.Default.GetString(tail, 39, 1);
                    jaristruct.m_Info8 = Encoding.Default.GetString(tail, 40, 1);
                    jaristruct.m_Info9 = Encoding.Default.GetString(tail, 41, 1);
                    jaristruct.m_Info10 = Encoding.Default.GetString(tail, 42, 1);
                    jaristruct.m_Info11 = Encoding.Default.GetString(tail, 43, 1);
                    jaristruct.m_Info12 = Encoding.Default.GetString(tail, 44, 32);
                    jaristruct.m_Info13 = Encoding.Default.GetString(tail, 76, 1);
                    jaristruct.m_Info14 = Encoding.Default.GetString(tail, 77, 48);
                    jaristruct.m_Info15 = Encoding.Default.GetString(tail, 125, 1);
                    jaristruct.m_Info16 = Encoding.Default.GetString(tail, 126, 2);
                    jaristruct.m_Info17 = Encoding.Default.GetString(tail, 128, 2);
                    jaristruct.m_Info18 = Encoding.Default.GetString(tail, 130, 19);
                    jaristruct.m_Info19 = Encoding.Default.GetString(tail, 149, 1);
                    jaristruct.m_Info20 = Encoding.Default.GetString(tail, 150, 5);
                    jaristruct.m_Info21 = Encoding.Default.GetString(tail, 155, 3);
                    jaristruct.m_Info22 = Encoding.Default.GetString(tail, 158, 3);
                    jaristruct.m_Info23 = Encoding.Default.GetString(tail, 161, 3);
                    jaristruct.m_Info24 = Encoding.Default.GetString(tail, 164, 1);
                }
            }
            catch { }
            return jaristruct;
        }
        public static void HKSDKInit()
        {
            try
            {
                CHCNetSDK.NET_DVR_Init();  //1.初始化
                CHCNetSDK.NET_DVR_SetConnectTime(3000, 3);    //2.设置连接超时时间ms
                CHCNetSDK.NET_DVR_SetReconnect(10000, 1);  //3.设置重连间隔ms
            }
            catch { }
        }
        public static void DHSDKInit()
        {
            try
            {
                DHClient.DHInit(null, IntPtr.Zero);//不要回调
                DHClient.DHSetConnectTime(3000, 3);
            }
            catch { }
        }
        public static void HKSDKDispose()
        {
            try
            {
                CHCNetSDK.NET_DVR_Cleanup();
            }
            catch { }
        }
        public static void DHSDKDispose()
        {
            try
            {
                DHClient.DHCleanup();
            }
            catch { }
        }

        public static bool PingIP(string ip)
        {
            bool ans = false;
            try
            {
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions(128, true);
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.Default.GetBytes(data);
                int timeout = 120;
                PingReply reply = pingSender.Send(System.Net.IPAddress.Parse(ip), timeout, buffer, options);
                if (reply != null)
                {
                    if (reply.Status == IPStatus.Success)
                    {
                        ans = true;
                    }
                }

            }
            catch { }
            return ans;
        }
        //public static bool SetConfig(string xmlFilePath, string nodeXPath, string innerText, string[] attributeName, string[] attributeValue)
        //{
        //    bool ans = false;
        //    XmlDocument doc = new XmlDocument();
        //    try
        //    {
        //        doc.Load(xmlFilePath);
        //        XmlNode node = doc.SelectSingleNode(nodeXPath);
        //        if (node != null)
        //        {
        //            node.InnerText = innerText;
        //            if (attributeName != null)
        //            {
        //                for (int i = 0; i < attributeName.Length; i++)
        //                {
        //                    node.Attributes[attributeName[i]].Value = attributeValue[i];
        //                }
        //            }
        //            doc.Save(xmlFilePath);
        //            ans = true;
        //        }

        //    }
        //    catch
        //    {
        //        ans = false;
        //    }
        //    finally
        //    {
        //        doc.RemoveAll();
        //    }
        //    return ans;

        //}

        public static bool SetConfig(string xmlFilePath, string nodeXPath, string innerText, string[] attributeName, string[] attributeValue)
        {
            bool ans = false;
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(xmlFilePath);
                XmlNode node = doc.SelectSingleNode(nodeXPath);

                if (node != null)
                {
                    //路口保存时需要保留原有的下属摄像机参数
                    //修改摄像机参数时要保存预置点参数
                    if (nodeXPath.Contains("road") || nodeXPath.Contains("camera"))
                    {
                        XmlNode father = node.ParentNode;
                        XmlNode newnode = node.CloneNode(true);
                        newnode.InnerText = innerText;
                        if (attributeName != null)
                        {
                            for (int i = 0; i < attributeName.Length; i++)
                            {
                                newnode.Attributes[attributeName[i]].Value = attributeValue[i];
                            }
                        }
                        if (node.HasChildNodes)
                        {
                            foreach (XmlNode child in node.ChildNodes)
                            {
                                newnode.AppendChild(child.CloneNode(true));
                            }
                        }
                        //father.AppendChild(newnode);
                        father.ReplaceChild(newnode, node);
                        doc.Save(xmlFilePath);
                        ans = true;
                    }
                    else
                    {
                        node.InnerText = innerText;
                        if (attributeName != null)
                        {
                            for (int i = 0; i < attributeName.Length; i++)
                            {
                                node.Attributes[attributeName[i]].Value = attributeValue[i];
                            }
                        }
                        doc.Save(xmlFilePath);
                        ans = true;
                    }
                }

            }
            catch
            {
                ans = false;
            }
            finally
            {
                doc.RemoveAll();
            }
            return ans;

        }
        
        
        public static bool DelConfig(string xmlFilePath, string nodeXPath, bool keepSelf)
        {
            bool ans = false;
            XmlDocument doc = new XmlDocument();
            try
            {
                if (File.Exists(xmlFilePath))
                {
                    doc.Load(xmlFilePath);
                    XmlNode node = doc.SelectSingleNode(nodeXPath);
                    if (node != null)
                    {
                        node.RemoveAll();
                        if (!keepSelf)
                        {
                            node.ParentNode.RemoveChild(node);
                        }
                        doc.Save(xmlFilePath);
                        ans = true;
                    }
                }
            }
            catch
            {
                ans = false;
            }
            finally
            {
                doc.RemoveAll();
            }
            return ans;

        }
        public static bool AddConfig(string xmlFilePath, string fatherNodeXPath, string nodename, string innerText, string[] attributeName, string[] attributeValue)
        {
            bool ans = false;
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(xmlFilePath);
                XmlNode fathernode = doc.SelectSingleNode(fatherNodeXPath);
                if (fathernode != null)
                {
                    XmlElement childnode = doc.CreateElement(nodename);
                    childnode.InnerText = innerText;
                    if (attributeName != null)
                    {
                        for (int i = 0; i < attributeName.Length; i++)
                        {
                            childnode.SetAttribute(attributeName[i], attributeValue[i]);
                        }
                    }
                    fathernode.AppendChild(childnode);
                    doc.Save(xmlFilePath);
                    ans = true;
                }
            }
            catch
            {
                ans = false;
            }
            finally
            {
                doc.RemoveAll();
            }
            return ans;

        }
        public static void DelRoad(string roadid)
        {
            try
            {
                int index = -1;
                if (gRoadList != null)
                {
                    for (int i = 0; i < gRoadList.Count; i++)
                    {
                        if (gRoadList[i].LKID == roadid)
                        {
                            if (gRoadList[i].JKCamList != null)
                            {
                                foreach (JKCamera cam in gRoadList[i].JKCamList)
                                {
                                    cam.ShutDown();
                                }
                                gRoadList[i].JKCamList.Clear();
                            }
                            index = i;
                            break;
                        }
                    }
                    if (index > -1)
                    {
                        gRoadList.RemoveAt(index);
                    }
                }
            }
            catch { }
        }
        public static void EditRoad(string roadid, string roadname,string gbdm,string bmbh,string user)
        {
            if (gRoadList != null)
            {
                foreach (RoadParam road in gRoadList)
                {
                    if (road.LKID == roadid)
                    {
                        road.LKID = roadid;
                        road.LKMC = roadname;
                        road.GBDM = gbdm;
                        road.BMBH = bmbh;
                        road.USER = user;
                        break;
                    }
                }
            }
        }
        public static void DelCam(string camip)
        {
            try
            {
                if (gRoadList != null)
                {
                    foreach (RoadParam road in gRoadList)
                    {
                        if (road.JKCamList != null)
                        {
                            for (int i = 0; i < road.JKCamList.Count; i++)
                            {
                                if (road.JKCamList[i].m_IP == camip)
                                {
                                    road.JKCamList[i].ShutDown();
                                    road.JKCamList.RemoveAt(i);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            catch { }
        }
        public static void EditCam(string camname, string camip, string camtype, string position, string bz,string deviceid,string username,string password)
        {
            if (gRoadList != null)
            {
                foreach (RoadParam road in gRoadList)
                {
                    if (road.JKCamList != null)
                    {
                        foreach (JKCamera cam in road.JKCamList)
                        {
                            if (cam.m_IP == camip)
                            {
                                cam.ShutDown();
                                cam.m_CameraName = camname;
                                cam.m_IP = camip;
                                cam.m_Type = (JKCAMERATYPE)Enum.Parse(typeof(JKCAMERATYPE), camtype);
                                cam.m_Position = (CAMERAPOSITION)Enum.Parse(typeof(CAMERAPOSITION), position);
                                cam.m_bz = bz;
                                cam.m_deviceID = deviceid;
                                cam.m_UserName = username;
                                cam.m_PassWord = password;
                            }
                        }
                    }
                }
            }
        }

        public static JKCamera FindCamByIP(string camip)
        {
            if (gRoadList != null)
            {
                foreach (RoadParam road in gRoadList)
                {
                    if (road.JKCamList != null)
                    {
                        foreach (JKCamera cam in road.JKCamList)
                        {
                            if (cam.m_IP == camip)
                            {
                                return cam;
                            }
                        }
                    }
                }
            }
            return null;
        }
        public static string ReadInnerTextString(string xmlFilePath, string paramName)
        {
            string ans = "";
            XmlDocument doc = new XmlDocument();
            try
            {
                if (File.Exists(xmlFilePath))
                {
                    doc.Load(xmlFilePath);
                    XmlNode node = doc.SelectSingleNode("descendant::" + paramName);
                    if (node != null)
                    {
                        ans = node.InnerText;
                    }
                }
            }
            catch
            {
                ans = "";
            }
            finally
            {
                doc.RemoveAll();
            }
            return ans;
        }

        public static string ReadInnerTextString(XmlDocument doc, string paramName)
        {
            string ans = "";
            try
            {
                if (doc != null)
                {
                    XmlNode node = doc.SelectSingleNode("descendant::" + paramName);
                    if (node != null)
                    {
                        ans = node.InnerText;
                    }
                }
            }
            catch
            {
                ans = "";
            }
            return ans;
        }

        public static string ReadAttributeString(string xmlFilePath, string paramName, string attributename)
        {
            string ans = "";
            XmlDocument doc = new XmlDocument();
            try
            {
                if (File.Exists(xmlFilePath))
                {
                    doc.Load(xmlFilePath);
                    XmlNode node = doc.SelectSingleNode("descendant::" + paramName);
                    if (node != null)
                    {
                        ans = node.Attributes[attributename].Value;
                    }
                }
            }
            catch
            {
                ans = "";
            }
            finally
            {
                doc.RemoveAll();
            }
            return ans;
        }

        public static string ReadAttributeString(XmlDocument doc, string paramName, string attributename)
        {
            string ans = "";
            try
            {
                if (doc != null)
                {
                    XmlNode node = doc.SelectSingleNode("descendant::" + paramName);
                    if (node != null)
                    {
                        ans = node.Attributes[attributename].Value;
                    }
                }
            }
            catch
            {
                ans = "";
            }
            return ans;
        }

        public static List<string> ReadAllAttributeName(XmlDocument doc, string nodeName)
        {
            List<string> ans = null;
            try
            {
                if (doc != null)
                {
                    XmlNode node = doc.SelectSingleNode("descendant::" + nodeName);
                    if ((node != null) && (node.Attributes != null))
                    {
                        ans = new List<string>();
                        foreach (XmlAttribute ab in node.Attributes)
                        {
                            ans.Add(ab.Name);
                        }
                    }
                }
            }
            catch
            {
                ans = null;
            }
            return ans;
        }

        public static List<string> ReadAllAttributeValue(XmlDocument doc, string nodeName)
        {
            List<string> ans = null;
            try
            {
                if (doc != null)
                {
                    XmlNode node = doc.SelectSingleNode("descendant::" + nodeName);
                    if ((node != null) && (node.Attributes != null))
                    {
                        ans = new List<string>();
                        foreach (XmlAttribute ab in node.Attributes)
                        {
                            ans.Add(ab.Value);
                        }
                    }
                }
            }
            catch
            {
                ans = null;
            }
            return ans;
        }

        public static List<string> ReadAllSubNodeAttribute0Names(XmlDocument doc, string nodeName)
        {
            List<string> ans = null;
            try
            {
                if (doc != null)
                {
                    XmlNode node = doc.SelectSingleNode("descendant::" + nodeName);
                    if ((node != null) && (node.ChildNodes != null))
                    {
                        ans = new List<string>();
                        foreach (XmlNode subnode in node.ChildNodes)
                        {
                            ans.Add(subnode.Attributes[0].Name);
                        }
                    }
                }
            }
            catch
            {
                ans = null;
            }
            return ans;
        }

        public static List<string> ReadAllSubNodeAttribute0Values(XmlDocument doc, string nodeName)
        {
            List<string> ans = null;
            try
            {
                if (doc != null)
                {
                    XmlNode node = doc.SelectSingleNode("descendant::" + nodeName);
                    if ((node != null) && (node.ChildNodes != null))
                    {
                        ans = new List<string>();
                        foreach (XmlNode subnode in node.ChildNodes)
                        {
                            ans.Add(subnode.Attributes[0].Value);
                        }
                    }
                }
            }
            catch
            {
                ans = null;
            }
            return ans;
        }

        public static List<string> ReadAllSubNodeInnerText(XmlNode thisnode)
        {
            List<string> ans = null;
            try
            {
                if ((thisnode != null) && (thisnode.ChildNodes != null) && (thisnode.ChildNodes.Count > 0))
                {
                    ans = new List<string>();
                    foreach (XmlNode node in thisnode)
                    {
                        ans.Add(node.InnerText);
                    }
                }
            }
            catch
            {
                ans = null;
            }
            return ans;
        }


        public static void DeleteLogFile(string logFolder, int saveDays)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(logFolder.Trim());
                if (di.Exists)
                {
                    FileInfo[] info = di.GetFiles("*.log2*", SearchOption.TopDirectoryOnly);
                    if (info != null)
                    {
                        foreach (FileInfo fi in info)
                        {
                            TimeSpan span = DateTime.Now - fi.LastWriteTime;
                            if (span.TotalDays > saveDays)
                            {
                                fi.Delete();
                            }
                        }

                    }
                }
            }
            catch { }
        }

        public static bool SetAppConfig(string key, string value)
        {
            bool ans = false;
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                if (config.AppSettings.Settings[key] != null)
                    config.AppSettings.Settings[key].Value = value;
                else
                    config.AppSettings.Settings.Add(key, value);

                
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                ans = true;
            }
            catch { ans = false; }
            return ans;
        }

        public static void CreateDefaultXmlFile(string xmlFilePath)
        {
            //<?xml version="1.0" encoding="GB2312"?>
            //<param>
            //  <JKSavePath>F:\监控抓拍图片</JKSavePath>
            //  <JKSaveBySubFolder enable="True">
            //  </JKSaveBySubFolder>
            //  <JKSubFolder 按年月日保存="" 按球机名称保存="">
            //  </JKSubFolder>
            //  <JKPicName>
            //    <NameMode 路口代码="">
            //    </NameMode>
            //    <NameMode 方向代码="">
            //    </NameMode>
            //    <NameMode 车道代码="">
            //    </NameMode>
            //    <NameMode 指定字符="-">
            //    </NameMode>
            //    <NameMode 年="">
            //    </NameMode>
            //    <NameMode 月="">
            //    </NameMode>
            //    <NameMode 日="">
            //    </NameMode>
            //    <NameMode 时="">
            //    </NameMode>
            //    <NameMode 分="">
            //    </NameMode>
            //    <NameMode 秒="">
            //    </NameMode>
            //    <NameMode 毫秒="">
            //    </NameMode>
            //    <NameMode 指定字符="-">
            //    </NameMode>
            //    <NameMode 车辆序号="">
            //    </NameMode>
            //    <NameMode 指定字符="-">
            //    </NameMode>
            //    <NameMode 抓拍张数="">
            //    </NameMode>
            //    <NameMode 指定字符="-">
            //    </NameMode>
            //    <NameMode 连拍序号="">
            //    </NameMode>
            //    <NameMode 图片格式=".jpg">
            //    </NameMode>
            //  </JKPicName>
            //  <cameratype typename="DAHUA" picwidth="1600" picheight="1240" city="睢宁" snapcount="3" picjionmode="不合成" bz="">
            //  </cameratype>
            //  <cameratype typename="HAIKANG" picwidth="2448" picheight="2080" city="睢宁" snapcount="3" picjionmode="不合成" bz="">
            //  </cameratype>
            //  <road roadid="20" gbdm="" roadname="测试路口">
            //    <camera cameraname="测试路口东球机" ip="192.168.1.108" type="DAHUA" position="东" speed="4" bz="">
            //    </camera>
            //    <camera cameraname="测试路口西球机" ip="192.168.1.122" type="HAIKANG" position="西" speed="4" bz="">
            //    </camera>
            //  </road>
            //</param>
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.RemoveAll();
                doc.AppendChild(doc.CreateXmlDeclaration("1.0", "GB2312", null));
                XmlElement root = doc.CreateElement("param");
                doc.AppendChild(root);

                XmlElement node = doc.CreateElement("JKSavePath");
                node.InnerText = "F:/监控抓拍图片";
                root.AppendChild(node);

                node = doc.CreateElement("JKSaveBySubFolder");
                node.SetAttribute("enable", "true");
                root.AppendChild(node);

                node = doc.CreateElement("LocalIP");
                node.InnerText = "192.168.1.107";
                root.AppendChild(node);

                node = doc.CreateElement("RegNode");
                node.InnerText = "JKZP";
                root.AppendChild(node);

                node = doc.CreateElement("JKSubFolder");
                node.SetAttribute("按年月日保存", "");
                node.SetAttribute("按球机名称保存", "");
                root.AppendChild(node);

                node = doc.CreateElement("JKPicName");
                root.AppendChild(node);
                XmlElement cnode;
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("路口代码", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("方向代码", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("车道代码", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("指定字符", "-"); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("年", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("月", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("日", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("时", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("分", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("秒", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("指定字符", "-"); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("毫秒", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("指定字符", "-"); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("抓拍张数", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("指定字符", "-"); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("连拍序号", ""); node.AppendChild(cnode);
                cnode = doc.CreateElement("NameMode"); cnode.SetAttribute("图片格式", ".jpg"); node.AppendChild(cnode);



                node = doc.CreateElement("cameratype");
                node.SetAttribute("typename", "DAHUA");
                node.SetAttribute("picwidth", "1600");
                node.SetAttribute("picheight", "1240");
                node.SetAttribute("city", "睢宁");
                node.SetAttribute("snapcount", "3");
                node.SetAttribute("picjionmode", "不合成");
                node.SetAttribute("bz", "");
                root.AppendChild(node);

                node = doc.CreateElement("cameratype");
                node.SetAttribute("typename", "HAIKANG");
                node.SetAttribute("picwidth", "2448");
                node.SetAttribute("picheight", "2080");
                node.SetAttribute("city", "睢宁");
                node.SetAttribute("snapcount", "3");
                node.SetAttribute("picjionmode", "不合成");
                node.SetAttribute("bz", "");
                root.AppendChild(node);

                //<road roadid="20" gbdm="" roadname="测试路口">
                //<camera cameraname="测试路口东球机" ip="192.168.1.108" type="DAHUA" position="东" speed="4" bz="">
                //</camera>
                //<camera cameraname="测试路口西球机" ip="192.168.1.122" type="HAIKANG" position="西" speed="4" bz="">
                //</camera>
                //</road>

                doc.Save(xmlFilePath);
                doc.RemoveAll();
            }
            catch { }
        }

        public static void ReLoad()
        {
            try
            {
                PP.gUserXml = PP.kdws.JKZP_UserList();
                bool admin = false;
                string xml = "";
                string road = "";
                string wfxwdm = "";
                string hpzl = "";
                string error = "";

                bool flag = PP.kdws.JKZP_Login(PP.gUser.username, PP.gUser.password, out admin, out xml, out road, out wfxwdm, out hpzl, out error);
                if (flag == true)
                {
                    File.WriteAllText(PP.gConfigFile, xml);
                    File.WriteAllText(PP.gRoadFile, road);
                    File.WriteAllText(PP.gWFXWDMFile, wfxwdm);
                    File.WriteAllText(PP.gHPZLFile, hpzl);


                    if (PP.gUser.username != "")
                    {
                        PP.SetAppConfig("LastUserName", PP.gUser.username);
                    }
                    PP.LoadSysParam();//加载系统参
                    PP.gCameraTypeList = PP.LoadCamType();//读取摄像机类型
                    PP.gRoadList = PP.LoadRoadParam();//读取路口和摄像机

                }
            }
            catch { }
        }

        public static bool SaveGZL(string username, int n)
        {
            bool ans = false;
            try
            {
                string head = DateTime.Now.ToString("yyyyMM");
                int dd = DateTime.Now.Day;

                if ((username != "") && (n > 0))
                {
                    string filename = PP.gWorkFolder + "\\Config\\" + username + ".txt";
                    if (File.Exists(filename))
                    {
                        bool found = false;
                        string[] lines = File.ReadAllLines(filename);
                        if ((lines != null) && (lines.Length > 0))
                        {
                            for (int j = 0; j < lines.Length; j++)
                            {
                                if (lines[j].StartsWith(head))
                                {
                                    found = true;
                                    string[] ss = lines[j].Split('-');
                                    if ((ss != null) && (ss.Length == 32))
                                    {
                                        int old = int.Parse(ss[dd]);
                                        int total = old + n;
                                        ss[dd] = total.ToString();
                                        string temp = head;
                                        for (int i = 1; i < ss.Length; i++)
                                        {
                                            temp += ("-" + ss[i]);
                                        }
                                        lines[j] = temp;
                                        //lines[j] = temp + "\r\n";
                                    }
                                    File.WriteAllLines(filename, lines);
                                    ans = true;
                                    break;
                                }
                            }

                            if (found == false)
                            {
                                string temp = head;
                                for (int i = 1; i < 32; i++)
                                {
                                    if (i == dd)
                                    {
                                        temp += ("-" + n.ToString());
                                    }
                                    else
                                    {
                                        temp += ("-0");
                                    }
                                }
                                string line = temp + "\r\n";
                                File.AppendAllText(filename, line);
                                ans = true;
                            }
                        }
                    }
                    else
                    {
                        string temp = head;
                        for (int i = 1; i < 32; i++)
                        {
                            if (i == dd)
                            {
                                temp += ("-" + n.ToString());
                            }
                            else
                            {
                                temp += ("-0");
                            }
                        }
                        string line = temp + "\r\n";
                        File.WriteAllText(filename, line);
                        ans = true;
                    }
                }
            }
            catch { ans = false; }
            return ans;
        }

        public static int GetGZL(string username, string from, string to)
        {
            int ans = 0;
            try
            {
                if ((username != "") && (from != "") && (to != ""))
                {
                    string filename = PP.gWorkFolder + "\\Config\\" + username + ".txt";
                    DateTime t1 = DateTime.Parse(from);
                    DateTime t2 = DateTime.Parse(to);

                    if (File.Exists(filename))
                    {
                        string[] lines = File.ReadAllLines(filename);
                        if ((lines != null) && (lines.Length > 0))
                        {
                            foreach (string line in lines)
                            {
                                if (line != "")
                                {
                                    int yyyy = int.Parse(line.Substring(0, 4));
                                    int MM = int.Parse(line.Substring(4, 2));
                                    string[] ss = line.Split('-');
                                    if ((ss != null) && (ss.Length == 32))
                                    {
                                        for (int i = 1; i < ss.Length; i++)
                                        {
                                            if (ss[i] != "0")
                                            {
                                                DateTime t = new DateTime(yyyy, MM, i);
                                                if ((t1 <= t) && (t <= t2))
                                                {
                                                    ans += (int.Parse(ss[i]));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch { ans = 0; }
            return ans;
        }
    }
}
